import * as React from "react"
import { type SearchParams } from "@/types/table"

import { getValidFilters } from "@/lib/data-table"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { Shell } from "@/components/shell"
import { getProcurementItems } from "@/lib/procurement-items/service"
import { ProcurementItemsTable } from "@/lib/procurement-items/table/procurement-items-table"
import { searchParamsCache } from "@/lib/procurement-items/validations"
import { InformationButton } from "@/components/information/information-button"

interface IndexPageProps {
  searchParams: Promise<SearchParams>
}

export default async function IndexPage(props: IndexPageProps) {
  const searchParams = await props.searchParams
  const search = searchParamsCache.parse(searchParams)

  const validFilters = getValidFilters(search.filters)

  const promises = Promise.all([
    getProcurementItems({
      ...search,
      filters: validFilters,
    }),
  ])

  return (
    <Shell className="gap-2">
      <div className="flex items-center justify-between space-y-2">
        <div className="flex items-center justify-between space-y-2">
          <div>
            <div className="flex items-center gap-2">
              <h2 className="text-2xl font-bold tracking-tight">
                1회성 품목 관리
              </h2>
              <InformationButton pagePath="evcp/procurement-items" />
            </div>
            <p className="text-muted-foreground">
              입찰에서 사용하는 1회성 품목을 등록하고 관리합니다.
            </p>
          </div>
        </div>
      </div>

      <React.Suspense
        fallback={
          <DataTableSkeleton
            columnCount={8}
            searchableColumnCount={1}
            filterableColumnCount={2}
            cellWidths={["10rem", "20rem", "8rem", "12rem", "6rem", "8rem", "10rem", "10rem"]}
            shrinkZero
          />
        }
      >
        <ProcurementItemsTable promises={promises} />
      </React.Suspense>
    </Shell>
  )
}
